package com.tf_cut.admin.backend.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mobile/upload")
@Slf4j
public class MobileShowFileController {

    @Value("${mobile.upload.path}")
    private String filePath;

    private String uploadPath="mobile/upload/";
    @Autowired
    private ResourceLoader resourceLoader;

    @RequestMapping("/{fileName}")
    public ResponseEntity upload(@PathVariable String fileName) {
        try {
            String fileFullPath = filePath +uploadPath+ fileName;
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            log.info("fileFullPath={}",fileFullPath);
            return ResponseEntity.ok(resourceLoader.getResource("file:" + fileFullPath));
        } catch (Exception e) {
            log.error("showFile error:{}",e.getMessage(),e);
            return ResponseEntity.notFound().build();
        }
    }

}
